package com.ruoyi.productProof.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.company.domain.CompanyScope;
import com.ruoyi.company.service.ICompanyScopeServer;
import com.ruoyi.product.domain.Product;
import com.ruoyi.product.service.ProductServer;
import com.ruoyi.productProof.domain.ProductProof;
import com.ruoyi.productProof.service.ProductProofService;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/business/productProof")
public class ProductProofController extends BaseController {

    private String prefix = "productProof";

    @Autowired
    private ProductProofService productProofService;

    @Autowired
    private ProductServer productServer;

    @Autowired
    private ICompanyScopeServer iCompanyScopeServer;

    @RequiresPermissions("business:productProof:view")
    @GetMapping("/{productId}")
    public String operlog(@PathVariable("productId") Long productId, ModelMap mmap) {
        mmap.addAttribute("productId", productId);
        mmap.addAttribute("deptId", ShiroUtils.getSysUser().getCompanyId());
        return prefix + "/proof";
    }

    /**
     * 查询采产品注册证列表
     */
    @RequiresPermissions("business:productProof:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProductProof productProof) {
        startPage();
        productProof.setDeptId(ShiroUtils.getSysUser().getCompanyId());
        List<ProductProof> list = productProofService.selectProductProofList(productProof);
        return getDataTable(list);
    }

    /**
     * 新增保存产品注册证
     */
    @RequiresPermissions("business:productProof:add")
    @Log(title = "新增保存产品注册证", businessType = BusinessType.INSERT)
    @GetMapping("/add/{productId}")
    public String add(@PathVariable("productId") Long productId, ModelMap mmap) {
        //查询生产厂商， 返回的数据 jsonArray数组
        mmap.addAttribute("productId", productId);
//        mmap.addAttribute("deptId", ShiroUtils.getSysUser().getDeptId());
        mmap.addAttribute("deptId", ShiroUtils.getSysUser().getCompanyId());
        return prefix + "/add";
    }

    /**
     * 新增保存产品注册证
     */
    @RequiresPermissions("business:productProof:add")
    @Log(title = "新增保存产品注册证", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Product product) {
        List<ProductProof> productProof = product.getProductProof();
        return toAjax(productProofService.insertProductProof(productProof));
    }

    /**
     * 新增保存产品注册证
     */
    @RequiresPermissions("business:productProof:add")
    @Log(title = "新增保存产品注册证", businessType = BusinessType.INSERT)
    @PostMapping("/adds")
    @ResponseBody
    public AjaxResult addSaves(ProductProof proof) {
        return toAjax(productProofService.insertProductProofs(proof));
    }

    /**
     * 修改产品信息
     */
    @GetMapping("/edit/{productProofId}")
    public String edit(@PathVariable("productProofId") Long productProofId, ModelMap mmap) {
        HashMap<String, Object> map = new HashMap<>();
//        map.put("deptId", ShiroUtils.getSysUser().getCompanyId());
        map.put("productProofId", productProofId);
        ProductProof productProof = productProofService.selectProductProofByMap(map);
        mmap.put("productProof", productProof);
        if (productProof != null) {
            //获取本公司的经营范围
            HashMap hashMap = new HashMap();
            hashMap.put("companyId", ShiroUtils.getSysUser().getCompanyId());
            hashMap.put("scopeClass", productProof.getManagementCategory());
            List<CompanyScope> list = iCompanyScopeServer.selectScopeByCompanyIdAndScopeClass(hashMap);

            mmap.put("companyScope", list);
        }
        return prefix + "/edit";
    }

    /**
     * 修改产品信息
     */
    @Log(title = "修改保存", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated ProductProof productProof) {
        return AjaxResult.success(productProofService.updateProductProof(productProof));
    }


    /**
     * 删除产品
     */
    @RequiresPermissions("business:productProof:remove")
    @PostMapping("/remove/{productProofId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("productProofId") Long productProofId) {
        return toAjax(productProofService.removeProduct(productProofId));
    }


    @PostMapping("/compare")
    @ResponseBody
    public int compare(@Param("deptId") Long deptId, @Param("medicalNumber") String medicalNumber) {
        ProductProof productProof = new ProductProof();
        productProof.setDeptId(deptId);
        productProof.setMedicalNumber(medicalNumber);
        List<ProductProof> productProofs = productProofService.selectProductProofList(productProof);
        if (productProofs.size() != 0) {
            return 1;
        } else {
            return 0;
        }
    }

    @PostMapping("/compares")
    @ResponseBody
    public AjaxResult compares(Date medicalStartTime, Long productId, Long productProofId) {
        ProductProof productProof = new ProductProof();
        productProof.setDeptId(ShiroUtils.getSysUser().getCompanyId());
        productProof.setProductId(productId);
        productProof.setProductProofId(productProofId);
        productProof.setMedicalStartTime(null);

        ProductProof isProductProof = productProofService.selectProductProof(productProof);
        if (isProductProof != null) {
            Date medicalEndTime = isProductProof.getMedicalEndTime();
            if (medicalEndTime.before(medicalStartTime)) {
                return AjaxResult.success();
            } else {
                return AjaxResult.error();
            }
        } else {
            return AjaxResult.success();
        }
    }

    @PostMapping("/changeTime")
    @ResponseBody
    public AjaxResult changeTime(Date medicalStartTime, Date medicalEndTime, Long productId, Long productProofId) {
        ProductProof productProof = new ProductProof();
        productProof.setDeptId(ShiroUtils.getSysUser().getCompanyId());
        productProof.setProductId(productId);
        productProof.setProductProofId(productProofId);
        productProof.setMedicalStartTime(medicalStartTime);
        productProof.setMedicalEndTime(medicalEndTime);

        List<ProductProof> isProductProof = productProofService.selectProductProofChange(productProof);

        if (isProductProof.size() != 0) {
            return AjaxResult.error();
        } else {
            return AjaxResult.success();
        }
    }

    /**
     * 删除产品注册证
     */
    @RequiresPermissions("business:productProof:delete")
    @Log(title = "删除产品注册证", businessType = BusinessType.DELETE)
    @PostMapping("/delete/{productId}")
    @ResponseBody
    public AjaxResult delete(@PathVariable("productId") Long productId) {
        List<Long> longs = productProofService.selectProductProofByProductId(productId);
        productProofService.deleteProductProofList(longs);
        return toAjax(1);
    }

    @GetMapping("/selectManagement/{managementCategory}")
    @ResponseBody
    public AjaxResult selectGetManagement(@PathVariable("managementCategory") Long managementCategory, ModelMap mmap) {
        mmap.addAttribute("managementCategory", productServer.selectManagementCategory(managementCategory));

        //获取本公司的经营范围
        HashMap hashMap = new HashMap();
        hashMap.put("companyId", ShiroUtils.getSysUser().getCompanyId());
        hashMap.put("scopeClass", managementCategory);
        List<CompanyScope> list = iCompanyScopeServer.selectScopeByCompanyIdAndScopeClass(hashMap);

        List<CompanyScope> users = new ArrayList<CompanyScope>();
        for (CompanyScope scope : list) {
            users.add(scope);
        }
        AjaxResult ajax = new AjaxResult();
        ajax.put("code", 200);
        ajax.put("value", list);
        return ajax;
    }
}